package com.withoutwords.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.withoutwords.R;
import com.withoutwords.engine.BitmapCache;
import com.withoutwords.engine.Common;
import com.withoutwords.engine.Game;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.MyInfo;
import com.withoutwords.engine.Round;
import com.withoutwords.engine.comminication.CommunicationManager;
import com.withoutwords.engine.comminication.IServerResultListener;
import com.withoutwords.engine.comminication.ServerRequests;
import com.withoutwords.engine.settings.Settings;
import com.withoutwords.engine.social.ISocialNetworkController;
import com.withoutwords.engine.social.SocialEngine;
import com.withoutwords.engine.social.SocialUser;
import com.withoutwords.engine.social.SocialNetwork;
import com.withoutwords.engine.social.listeners.ISocialFriendListListener;
import com.withoutwords.ui.custom.MyCustomButton;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

public class GamePlayersActivity extends BaseActivity {

	ISocialNetworkController fbController;
	GameController game;
	Handler uiHandler;
	ArrayList<String> currentGamePlayer;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_players);
		playSound(soundResId);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		game = GameController.getInstance();
		if (game != null) {
			currentGamePlayer = game.getCurrentGamePlayerNames();
			updateListWithFriends(currentGamePlayer);
		}
	}

	@Override
	protected void setBitmaps() {
		// TODO Auto-generated method stub
		super.setBitmaps();
		View v = findViewById(R.id.lstPlayers);
		v.setBackgroundDrawable(BitmapCache.getCacheInstance().getDrawable(this,
				R.drawable.blackboard));
		v.setVisibility(View.VISIBLE);
	}

	@Override
	protected void unsetBitmaps() {
		// TODO Auto-generated method stub
		super.unsetBitmaps();
		View ly = (ViewGroup) findViewById(R.id.lstPlayers);
		if (ly != null) {
			ly.setVisibility(View.GONE);
			ly.setBackgroundDrawable(null);
			releaseBitmaps();
		}
	}

	@Override
	protected void releaseBitmaps() {
		// TODO Auto-generated method stub
		super.releaseBitmaps();

		BitmapCache.getCacheInstance().deleteDrawable(R.drawable.blackboard);

	}

	@Override
	protected void onResume() {
		Game currentGame = GameController.getInstance().getCurrentGame();
		if (currentGame != null) {
			Round round = currentGame.getCurrentRound();

			if (round != null && round.isRoundSolved()) {
				finish();

			}
		}
		// TODO Auto-generated method stub
		super.onResume();
	}

	void updateListWithFriends(ArrayList<String> friendlist) {
		// set layout visible to begin animation
		// findViewById(R.id.friendListLayout).setVisibility(View.VISIBLE);

		// this.friendList = friendlist;
		ListView lv = (ListView) findViewById(R.id.lstPlayers);
		if (lv != null && friendlist != null) {
			lv.setAdapter(new ArrayAdapter<String>(GamePlayersActivity.this,
					android.R.layout.simple_list_item_1, friendlist));// (ArrayList<SocialUser>)
																		// response.get("friendList")));

			lv.invalidate();
		}

		// }
	}

}
